//
//  HELCoursePicker.m
//  HandheldELearn
//
//  Created by wsj2015 on 2017/1/10.
//  Copyright © 2017年 陈武琦. All rights reserved.
//

#import "HELCoursePicker.h"
@class WCBorderView;

@interface HELCoursePicker ()


@property (weak, nonatomic) IBOutlet WCBorderView *bg_view;

@property (weak, nonatomic) IBOutlet UIView *sort_bg;
@property (weak, nonatomic) IBOutlet UIView *scoreType_bg;

@property (weak, nonatomic) IBOutlet UIView *cate_bg;

@property (nonatomic,  assign) HELSortType sort;
@property (nonatomic,  assign) HELCateType cate;
@property (nonatomic,  assign) HELScoreType scoreType;





@end

@implementation HELCoursePicker


+ (instancetype)pickerWithDefultSortType:(HELSortType)sort scoreType:(HELScoreType)scoreType cateType:(HELCateType)cateType
{
    HELCoursePicker *picker = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:0].lastObject;
    picker.sort = sort;
    picker.scoreType =scoreType;
    picker.cate =cateType;
  
    [picker setupSubviews];
    return picker;
}

- (void)setupSubviews{
    
    //这里 先要修改status_bg 里的按钮的tag值 去对应
//    
//    [self.status_bg.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
//        if (!obj.tag) {
//            [( UILabel *)obj setText:@"所属"];
//        }
//        if (obj.tag == 199) {
//            obj.tag = 200;
//        }else if (obj.tag == 200){
//             obj.tag = 201;
//            [(UIButton *)obj setTitle:@"院内" forState:UIControlStateNormal];
//        }else if (obj.tag == 202){
//             obj.tag = 202;
//            [(UIButton *)obj setTitle:@"公开" forState:UIControlStateNormal];
//        }
//    }];
    
    [@[self.sort_bg,self.scoreType_bg,self.cate_bg] enumerateObjectsUsingBlock:^(UIView *  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        
        [obj.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger jdx, BOOL * _Nonnull stop) {
            if (jdx ) {
                obj.layer.borderColor = kBorderColor.CGColor;
                obj.layer.borderWidth = 1;
                wsj_cornerRadius(obj, 4);
                
                
                if (idx == 0) {
                    if (obj.tag-200 == self.sort) {
                        [self setBtnToSelectdStatuWithBtn:obj];
                    }
                }else if(idx==1){
                    if (obj.tag-200 == self.scoreType) {
                        [self setBtnToSelectdStatuWithBtn:obj];
                    }
                }else{
                    if (obj.tag-200 == self.cate) {
                        [self setBtnToSelectdStatuWithBtn:obj];
                    }
                }
                
            }
            
        }];
        
        
    }];
    

}

- (void)setBtnToSelectdStatuWithBtn:(UIButton *)btn{
    [btn  setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [btn setBackgroundColor:HexRGB_WSJ(240, 199, 205)];
    btn.layer.borderColor = [UIColor redColor].CGColor;

}
- (void)setBtnToUnelectdStatuWithBtn:(UIButton *)btn{
    [btn  setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [btn setBackgroundColor:HexRGB_WSJ(242, 242, 246)];
     btn.layer.borderColor = kBorderColor.CGColor;
}


- (IBAction)confirmAction:(id)sender {
    if (self.confirm) {
         self.confirm(self.sort, self.scoreType, self.cate);
    }
   
    
  
   
    
    
    
}

- (IBAction)cancelAction:(id)sender {
    
       self.cancel();
}

- (IBAction)sortAction:(UIButton *)sender {
    if (sender.tag-200 == self.sort) {
        return;
    }
    UIButton  *lastBtn =[self.sort_bg viewWithTag:self.sort+200];
    
    [self setBtnToUnelectdStatuWithBtn:lastBtn];
    [self setBtnToSelectdStatuWithBtn:sender];
    self.sort = sender.tag -200;
    
}

- (IBAction)typeAction:(UIButton*)sender {
  
    
    
    if (sender.tag-200 == self.scoreType) {
        return;
    }
    UIButton  *lastBtn =[self.scoreType_bg viewWithTag:self.scoreType+200];
    
    [self setBtnToUnelectdStatuWithBtn:lastBtn];
    [self setBtnToSelectdStatuWithBtn:sender];
    self.scoreType = sender.tag - 200;
}

- (IBAction)statusAction:(UIButton *)sender {
    if (sender.tag-200 == self.cate) {
        return;
    }
    UIButton  *lastBtn =[self.cate_bg viewWithTag:self.cate+200];
    
    [self setBtnToUnelectdStatuWithBtn:lastBtn];
    [self setBtnToSelectdStatuWithBtn:sender];
    self.cate = sender.tag -200;
    
    
}


@end
